###########################################
#
#  Octojam 8 Greeting
#
#  This program is a greeting message for
#  the 2021 Octojam. Join us anytime during
#  the month of October and make a game or
#  program for CHIP-8!
#
#  Find out more at octojam.com or
#  https://itch.io/jam/octojam-8
#
###########################################

: main
	# draw "octojam" logo
	i  := title
	v1 := 1
	vd := 4  # frame delay
	ve := 15 # sprite stride
	loop
		sprite v0 v1 15
		i  += ve
		v0 += 8
		if v0 != 64 then
	again
	# animate "8"
	v0 := 24
	v1 := 32
	v2 := 17
	i := eightinit
	number
	loop
		# unrolled loop for simplicity:
		number
		number
		number
		number
		number
		number
		number
		# random eights
		vf := 8
		i := hex vf
		va := random 0b1110 # 0,2,4,6,8,10,12,14
		vb := random 1      # 0,1
		if vb != 0 then va += 42
		va += 2
		vb := random 0b110  # 0,2,4,6
		vb += 18
		sprite va vb 5
		# reset for the unrolled loop
		i := eights
	again
: number
	sprite v0 v2 15
	i  += ve
	sprite v1 v2 15
	i  += ve
	loop
		vf := delay
		if vf != 0 then
	again
	delay := vd
;

: title # horizontal strip of 8x15 sprites
	0x1F 0x3F 0x7F 0x78 0xF0 0xE0 0xE0 0xE0 0xE0 0xF0 0x70 0x78 0x3F 0x1F 0x0F
	0x80 0xE0 0xE1 0xF3 0x77 0x77 0x36 0x36 0x36 0x37 0x77 0xF3 0xE1 0xE0 0xC0
	0x1F 0x1F 0xEF 0xF7 0xF7 0x37 0x07 0x03 0x1B 0x3B 0xFB 0xF3 0xE1 0x00 0x00
	0xE7 0xEF 0xDF 0x3C 0x38 0x30 0x70 0x70 0x70 0x70 0x38 0x3C 0x1F 0x1F 0x07
	0xE7 0xF7 0xFB 0x78 0x3C 0x3C 0x1C 0x1D 0x39 0x39 0x73 0xF7 0xEF 0xDE 0x80
	0xFD 0xFD 0xFB 0xE7 0xE7 0xE6 0xEE 0xCF 0xCF 0xDC 0xDC 0xB8 0x30 0x60 0x00
	0x90 0x98 0x9E 0xDF 0xDF 0xEF 0x6F 0xEE 0xF6 0xF7 0x77 0x33 0x33 0x27 0x00
	0x70 0xF0 0xF8 0xF8 0xF8 0xDC 0xDC 0xCE 0x4E 0x0E 0x0F 0x87 0x87 0x81 0x00

: eightinit # left and right sprites are interleaved
	0x07 0x0F 0x18 0x18 0x18 0x1C 0x1F 0x07 0x03 0x03 0x0C 0x18 0x1C 0x1F 0x0F
		0xC0 0xF0 0x70 0x18 0x18 0x38 0x60 0xC0 0xE0 0x70 0x18 0x18 0x38 0xF8 0xF0
: eights # each frame is pre-xored with its predecessor
	0x00 0x0A 0x10 0x10 0x14 0x18 0x18 0x06 0x00 0x05 0x02 0x14 0x10 0x11 0x08
		0x00 0x10 0x90 0x78 0x78 0x58 0xA0 0x00 0x00 0x80 0x68 0x28 0x08 0x98 0x10
	0x04 0x06 0x0D 0x0C 0x08 0x02 0x04 0x02 0x04 0x00 0x08 0x0A 0x0B 0x09 0x04
		0x40 0x20 0x20 0xA0 0xA0 0xA0 0x00 0x00 0xA0 0xD0 0x50 0x10 0x50 0xA0 0x20
	0x00 0x02 0x04 0x07 0x07 0x07 0x02 0x00 0x04 0x05 0x05 0x07 0x06 0x04 0x00
		0x80 0x40 0x40 0x40 0x40 0x40 0xC0 0xC0 0xC0 0xA0 0xA0 0xA0 0xE0 0x40 0xC0
	0x02 0x02 0x02 0x00 0x00 0x02 0x02 0x00 0x01 0x07 0x07 0x05 0x07 0x00 0x00
		0xC0 0x40 0x20 0xA0 0xA0 0xE0 0xC0 0xC0 0x60 0xE0 0xE0 0xE0 0x60 0x60 0xC0
	0x02 0x04 0x04 0x05 0x05 0x05 0x00 0x00 0x05 0x0B 0x0A 0x08 0x0A 0x05 0x04
		0x20 0x60 0xB0 0x30 0x10 0x40 0x20 0x40 0x20 0x00 0x10 0x50 0xD0 0x90 0x20
	0x00 0x08 0x09 0x1E 0x1E 0x1A 0x05 0x00 0x00 0x01 0x16 0x14 0x10 0x19 0x08
		0x00 0x50 0x08 0x08 0x28 0x18 0x18 0x60 0x00 0xA0 0x40 0x28 0x08 0x88 0x10
	0x04 0x00 0x16 0x00 0x00 0x00 0x19 0x04 0x04 0x0D 0x14 0x00 0x00 0x00 0x00
		0x20 0x00 0x68 0x00 0x00 0x00 0x98 0x20 0x20 0xB0 0x28 0x00 0x00 0x00 0x00
